<?php
namespace app\index\controller;

class Api extends Base2
{
    
    
    function bind(){
        $socket = new \app\index\model\Socket();
        return $socket->bind($this->uid);
    }
    
    function getResult(){
        $jccount = new \app\index\model\Jccount();
        return json($jccount->readjc());
    }
    function kaijaing(){
        $file = "jclock.php";
        $fp = fopen($file, "w");
        if (flock($fp, LOCK_EX|LOCK_NB)) {
        $jccount = new \app\index\model\Jccount();
         $jccount->kaijaing();
        }
        flock($fp, LOCK_UN); // 解锁
        fclose($fp);
    }
    function openGame(){
        $jccount = new \app\index\model\Jccount();
        return $jccount->openjc();
    }
    function getNow(){
        $jccount = new \app\index\model\Jccount();
        $res = $jccount->getLock();
        return $this->returnOk($res);
    }
    
    
    /**
     * 充值
     * @return \think\response\Json
     */
    public function ajaxchongzhi()
    {
        $user = new \app\index\model\User();
        $isinblack = $user->isinblack($this->uid);
        if(!$isinblack){
            return $this->returnError("用户状态异常");
        }
        $id = I('post.shopId');
        $shop = new \app\index\model\Shop();
        $res = $shop->getShop($id);
        if (!$res){
            return $this->param_error();
        }
        $userinfo = $user->getUserByUserId($this->uid);
        $wx = new \app\index\model\Wx();
        $payinfo = $wx->pay($userinfo['id'], $userinfo['openid'], $res['money']);
        $datass['data']=$payinfo;
        $datass['money']=$res['money'];
        return $this->returnOk($datass);
    }
    
    
    public function applyDaili(){
        
        $user = new \app\index\model\User();
        $user->applyDaili($this->uid);
        return $this->returnOk();
    }
    
    public function xiazhu(){
        $jccount = new \app\index\model\Jccount();
        $now = $jccount->getLock();
        if ($now['times']<10){
            return $this->returnAlert("即将开奖，不能下注");
        }
        $user = new \app\index\model\User();
        $isinblack = $user->isinblack($this->uid);
        if(!$isinblack){
            return $this->returnError("用户状态异常");
        }
        $types= new \app\index\model\Types();
        $min_and_max = $types->get_min_and_max();
        $data['xiazhu_type']  = I('post.xiazhutype');
        $data['type']  = I('post.type');
        $data['money']  = I('post.money');
        $num  = I('post.num');
        if (!$data['xiazhu_type']||!is_numeric($data['money'])){
            return $this->param_error();
        }
        $data['money'] = intval($data['money'])*100;
        if($data['money']<$min_and_max['min_value']){
            return $this->returnAlert("最小押注".$min_and_max['min_value']);
        }
        if($data['money']>$min_and_max['max_value']){
            return $this->returnAlert("最大押注".$min_and_max['max_value']);
        }
        $isin = $user->getUserByUserId($this->uid);
        if ($isin['is_black']==1){
            return $this->returnAlert("用户状态异常");
        }
        if ($isin['money']<$data['money']){
            return $this->returnAlert("余额不足");
        }
        if ($data['xiazhu_type']==8){
            if ($num===''||$num===null){
                return $this->param_error();
            }
            $data['num'] = "".$num;
        }
        $data['user_id'] = $this->uid;
        
        $type = null;
        switch ($data['xiazhu_type']) {
            case 1:
            case 2:
                $type = $types->getType(1);
                $data['type'] = 1;
            break;
            case 3:
            case 4:
            case 5:
            case 6:

                $type = $types->getType(2);
                $data['type'] = 2;
            break;
            case 8:

                $type = $types->getType(3);
                $data['type'] = 3;
            break;
            case 9:

                $type = $types->getType(9);
                $data['type'] = 1;
            break;
            
            default:
                
            return $this->param_error();
        }

        $data['peilv'] = $type['peilv'];
        
        
        
        
        
        $data['qi_id'] = $now['id'];
        $jc = new \app\index\model\Jc();
        $jc->addJCDetails($data);
        $jc->addPerson($data['qi_id'], $this->uid);
        $jc->addMoney($data['qi_id'], $data['money']);
        $realmoney = $user->updateMoney($this->uid, $data['money']);
        $sahngji = $user->getshangji2($this->uid);  
        foreach ($sahngji as $sahngjiTmp){
            $sahngjiTmp['qi_id'] = $data['qi_id'];
            $sahngjiTmp['money'] = $data['money']*$sahngjiTmp['baifenbi']/100;
            $user->addYongjin($sahngjiTmp);
        }
        $socket = new \app\index\model\Socket();
        $socket->sendadmin_moneychange();
        return $this->returnOk(round($realmoney/100,2));
    }
    
	function getJcHis(){
		$page = I('post.page');
		if(!$page){
			$page=1;
		}
		$jc = new \app\index\model\Jc();
        $res = $jc->getJcAllHis($page);
		return $this->returnOk($res);
	}
	function getJcMeHis(){
	    $page = I('post.page');
	    if(!$page){
	        $page=1;
	    }
	    $jc = new \app\index\model\Jc();
	    $res = $jc->getMyJCAll($this->uid,$page);
	    return $this->returnOk($res);
	}
	function getPayHis(){
	    $page = I('post.page');
	    if(!$page){
	        $page=1;
	    }
	    $jc = new \app\index\model\Payhistory();
	    $res = $jc->getHistoryAll($this->uid,$page);
	    return $this->returnOk($res);
	}
	function getDHHis(){
	    $page = I('post.page');
	    if(!$page){
	        $page=1;
	    }
	    $jc = new \app\index\model\Duihuan();
	    $res = $jc->getHistoryAll($this->uid,$page);
	    return $this->returnOk($res);
	}
	function getyjHis(){
	    $page = I('post.page');
	    if(!$page){
	        $page=1;
	    }
	    $jc = new \app\index\model\Yongjin();
	    $res = $jc->getYongjinAll2($this->uid,$page);
	    return $this->returnOk($res);
	}
	
	
	function getMymoney(){
	    if (!$this->uid){
	        return $this->param_error();
	    }
	    $user = new \app\index\model\User();
	    return $this->returnOk($user->getMyMoney($this->uid));
	}
	
	
}
